﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;


/// <summary>
/// Summary description for Page
/// </summary>
public class Page : System.IDisposable
{
    System.Data.DataSet dataset = null;

    List<String> errors = new List<String>();
    System.Data.SqlClient.SqlConnection myConnection = null;
    int commandTimeout = 300;

    public void Dispose()
    {
        if (myConnection != null)
            myConnection.Close();
    }

    bool QuerySql(String connectionString, String commandTimeout)
    {
        try
        {
            errors.Clear();

            myConnection = new SqlConnection(connectionString);
            if (myConnection != null)
            {
                myConnection.Open();
                if (myConnection.State == System.Data.ConnectionState.Open)
                {
                    int seconds;
                    if (int.TryParse(commandTimeout, out seconds))
                        this.commandTimeout = seconds;
                    return true;
                }
                else
                {
                    logError(myConnection.State.ToString());
                }
            }
            else
            {
                logError(connectionString);
            }
        }
        catch (SqlException e)
        {
            logError(e.Message);
        }
        catch (InvalidOperationException e)
        {
            logError(e.Message);
        }
        catch (Exception e)
        {
            logError(e.Message);
        }
        return false;
    }

    Boolean IsStatusBad()
    {
        return myConnection == null || errors.Count != 0;
    }

    int Fill( String commandText)
    {
        if (IsStatusBad())
            return 0;

        dataset = new System.Data.DataSet();

        System.Data.SqlClient.SqlDataAdapter adapter =
            new System.Data.SqlClient.SqlDataAdapter(commandText, myConnection);

        adapter.SelectCommand.CommandTimeout = commandTimeout;

        int rowsReturned = adapter.Fill(dataset, "DoesntMatter");

        myConnection.Close();
        myConnection = null;

        return rowsReturned;
    }

    void logError(String err)
    {
        errors.Add(err);
    }

    public String pageCaption=null;
    public String rowLabelName = null;
    public String[] textColumnNames = null;
    public String[] numericColumnNames = null;
    public String[][] textColumnValues = null;
    public Double[,] numericColumnValues ;
    public Boolean[] numericColumnSelections ;

    public Page()
    {
    }
    public String GetPageCaption()
    {
        String caption = dataset.ToString();
        return caption;
    }
    public String GetRowLabelName()
    {
        for (int dbColIndex = 0, dbColCount = dataset.Tables[0].Columns.Count;
                        dbColIndex < dbColCount; dbColIndex++)
        {
            const String prefix = "rowlabel_";
            String dbColumnName = dataset.Tables[0].Columns[dbColIndex].ColumnName;
            if (dbColumnName.StartsWith( prefix, StringComparison.CurrentCultureIgnoreCase))
                return dbColumnName.Substring(prefix.Length);
        }
        return "";
    }
    public int GetTextColumnCount()
    {
        int colCount = 0;
        for (int dbColIndex = 0, dbColCount = dataset.Tables[0].Columns.Count;
                        dbColIndex < dbColCount; dbColIndex++)
        {
            const String prefix = "text_";
            String dbColumnName = dataset.Tables[0].Columns[dbColIndex].ColumnName;
            if (dbColumnName.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase))
                colCount++;
        }
        return colCount;
    }
    public String GetTextColumnName(int textIndex)
    {
        int colCount = 0;
        for (int dbColIndex = 0, dbColCount = dataset.Tables[0].Columns.Count;
                        dbColIndex < dbColCount; dbColIndex++)
        {
            const String prefix = "text_";
            String dbColumnName = dataset.Tables[0].Columns[dbColIndex].ColumnName;
            if (dbColumnName.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase))
            {
                colCount++;
                if (colCount == textIndex)
                {
                    return dbColumnName.Substring(prefix.Length);
                }
            }
        }
        return "";
    }
    public int GetNumericColumnCount()
    {
        int colCount = 0;
        for (int dbColIndex = 0, dbColCount = dataset.Tables[0].Columns.Count;
                        dbColIndex < dbColCount; dbColIndex++)
        {
            const String prefix = "numeric_";
            String dbColumnName = dataset.Tables[0].Columns[dbColIndex].ColumnName;
            if (dbColumnName.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase))
                colCount++;
        }
        return colCount;
    }
    public String GetNumericColumnName(int columnIndex)
    {
        int colCount = 0;
        for (int dbColIndex = 0, dbColCount = dataset.Tables[0].Columns.Count;
                        dbColIndex < dbColCount; dbColIndex++)
        {
            const String prefix = "numeric_";
            String dbColumnName = dataset.Tables[0].Columns[dbColIndex].ColumnName;
            if (dbColumnName.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase))
            {
                colCount++;
                if (colCount == columnIndex)
                {
                    return dbColumnName.Substring(prefix.Length);
                }
            }
        }
        return "";
    }
    public Object GetRowLabelValue(int rowIndex)
    {
        for (int dbColIndex = 0, dbColCount = dataset.Tables[0].Columns.Count;
                        dbColIndex < dbColCount; dbColIndex++)
        {
            const String prefix = "rowlabel_";
            String dbColumnName = dataset.Tables[0].Columns[dbColIndex].ColumnName;
            if (dbColumnName.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase))
            {
                System.Data.DataRow thisRow = dataset.Tables[0].Rows[rowIndex];
                Object rowLabel = thisRow.ItemArray[dbColIndex];
                return rowLabel;
            }
        }

        return "";
    }
    public Object GetTextColumnValue(int rowIndex, int textIndex)
    {
        int colCount = 0;
        for (int dbColIndex = 0, dbColCount = dataset.Tables[0].Columns.Count;
                        dbColIndex < dbColCount; dbColIndex++)
        {
            const String prefix = "text_";
            String dbColumnName = dataset.Tables[0].Columns[dbColIndex].ColumnName;
            if (dbColumnName.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase))
            {
                colCount++;
                if (colCount == textIndex)
                {
                    System.Data.DataRow thisRow = dataset.Tables[0].Rows[rowIndex];
                    Object textValue = thisRow.ItemArray[dbColIndex];
                    return textValue;
                }
            }
        }

        return textColumnValues[rowIndex][textIndex];
    }
    public Object GetNumericCellContents(int rowIndex, int columnIndex)
    {
        int colCount = 0;
        for (int dbColIndex = 0, dbColCount = dataset.Tables[0].Columns.Count;
                        dbColIndex < dbColCount; dbColIndex++)
        {
            const String prefix = "numeric_";
            String dbColumnName = dataset.Tables[0].Columns[dbColIndex].ColumnName;
            if (dbColumnName.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase))
            {
                colCount++;
                if (colCount == columnIndex)
                {
                    System.Data.DataRow thisRow = dataset.Tables[0].Rows[rowIndex];
                    if (thisRow.ItemArray.Length >= dbColIndex)
                    {
                        Object cellValue = thisRow.ItemArray[dbColIndex];
                        return cellValue;
                    }
                }
            }
        }

        return null;
    }


    public void Initialize( String connectionString, String commandTimeout, String commandText )
    {
        QuerySql(connectionString, commandTimeout);
        if (IsStatusBad() == false)
        {
            int rowsReturned = Fill( commandText );
        }
    }
}
